package com.hopu.jq_day04.util;

import org.apache.ibatis.session.SqlSession;

public class GetSqlSession {
	
	private static ThreadLocal<SqlSession> tl = new ThreadLocal<SqlSession>();

    /**
     * 获取SqlSession
     * @return sqlSession
     */
    public static SqlSession getSqlSession(){
        SqlSession sqlSession = tl.get();
        if (sqlSession == null){
            sqlSession = GetSqlSessionFactory.getSqlSessionFactory().openSession();
            tl.set(sqlSession);
        }
        return sqlSession;
    }

    /**
     *  提交事物
     */
    public static void commit(){
        if (tl.get() != null){
            System.out.println("coommit");
            tl.get().commit();
            tl.get().close();
            tl.set(null);
        }
    }

    /**
     *  回滚事务
     */
    public static void rollback(){
        if (tl.get() != null){
            System.out.println("roolback.");
            tl.get().rollback();
            tl.get().close();
            tl.set(null);
        }
    }
}
